function t() {
    try {
        var t = "s" + a();
        return wx.setStorageSync(s.prefix + "ssid", t), t;
    } catch (t) {}
}

function a(t) {
    for (var a = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ], e = 10; 1 < e; e--) {
        var n = Math.floor(10 * Math.random()), r = a[n];
        a[n] = a[e - 1], a[e - 1] = r;
    }
    for (e = n = 0; 5 > e; e++) n = 10 * n + a[e];
    return (t || "") + (n + "") + +new Date();
}

function e() {
    try {
        var t = getCurrentPages(), a = "/";
        return 0 < t.length && (a = t.pop().__route__), a;
    } catch (t) {
        console.log("get current page path error:" + t);
    }
}

function n() {
    var n = {
        dm: "wechat.apps.xx",
        url: encodeURIComponent(e() + i(u.Data.pageQuery)),
        pvi: "",
        si: "",
        ty: 0
    };
    return n.pvi = function() {
        var t = function() {
            try {
                return wx.getStorageSync(s.prefix + "auid");
            } catch (t) {}
        }();
        return t || (t = function() {
            try {
                var t = a();
                return wx.setStorageSync(s.prefix + "auid", t), t;
            } catch (t) {}
        }(), n.ty = 1), t;
    }(), n.si = function() {
        var a = function() {
            try {
                return wx.getStorageSync(s.prefix + "ssid");
            } catch (t) {}
        }();
        return a || (a = t()), a;
    }(), n;
}

function r() {
    var t = function() {
        var t = wx.getSystemInfoSync();
        return {
            adt: encodeURIComponent(t.model),
            scl: t.pixelRatio,
            scr: t.windowWidth + "x" + t.windowHeight,
            lg: t.language,
            fl: t.version,
            jv: encodeURIComponent(t.system),
            tz: encodeURIComponent(t.platform)
        };
    }();
    return function(t) {
        wx.getNetworkType({
            success: function(a) {
                t(a.networkType);
            }
        });
    }(function(t) {
        try {
            wx.setStorageSync(s.prefix + "ntdata", t);
        } catch (t) {}
    }), t.ct = wx.getStorageSync(s.prefix + "ntdata") || "4g", t;
}

function o() {
    var t, a = u.Data.userInfo, e = [];
    for (t in a) a.hasOwnProperty(t) && e.push(t + "=" + a[t]);
    return a = e.join(";"), {
        r2: s.app_id,
        r4: "wx",
        ext: "v=" + s.version + (null !== a && "" !== a ? ";ui=" + encodeURIComponent(a) : "")
    };
}

function i(t) {
    if (!s.stat_param || !t) return "";
    t = function(t) {
        if (1 > s.ignore_params.length) return t;
        var a, e = {};
        for (a in t) 0 <= s.ignore_params.indexOf(a) || (e[a] = t[a]);
        return e;
    }(t);
    var a, e = [];
    for (a in t) e.push(a + "=" + t[a]);
    return 0 < e.length ? "?" + e.join("&") : "";
}

var s = {
    app_id: "",
    event_id: "",
    api_base: "",
    prefix: "_mta_",
    version: "1.3.10",
    stat_share_app: !1,
    stat_pull_down_fresh: !1,
    stat_reach_bottom: !1,
    stat_param: !0
}, u = {
    App: {
        init: function(a) {
            "appID" in a && (s.app_id = a.appID), "eventID" in a && (s.event_id = a.eventID), 
            "statShareApp" in a && (s.stat_share_app = a.statShareApp), "statPullDownFresh" in a && (s.stat_pull_down_fresh = a.statPullDownFresh), 
            "statReachBottom" in a && (s.stat_reach_bottom = a.statReachBottom), "ignoreParams" in a && (s.ignore_params = a.ignoreParams), 
            "statParam" in a && (s.stat_param = a.statParam), t();
            try {
                "lauchOpts" in a && (u.Data.lanchInfo = a.lauchOpts, u.Data.lanchInfo.landing = 1);
            } catch (a) {}
            "autoReport" in a && a.autoReport && function() {
                var t = Page;
                Page = function(a) {
                    var n = a.onLoad;
                    a.onLoad = function(t) {
                        n && n.call(this, t), u.Data.lastPageQuery = u.Data.pageQuery, u.Data.pageQuery = t, 
                        u.Data.lastPageUrl = u.Data.pageUrl, u.Data.pageUrl = e(), u.Data.show = !1, u.Page.init();
                    }, t(a);
                };
            }();
        }
    },
    Page: {
        init: function() {
            var t = getCurrentPages()[getCurrentPages().length - 1];
            t.onShow && function() {
                var a = t.onShow;
                t.onShow = function() {
                    if (!0 === u.Data.show) {
                        var t = u.Data.lastPageQuery;
                        u.Data.lastPageQuery = u.Data.pageQuery, u.Data.pageQuery = t, u.Data.lastPageUrl = u.Data.pageUrl, 
                        u.Data.pageUrl = e();
                    }
                    u.Data.show = !0, u.Page.stat(), a.apply(this, arguments);
                };
            }(), s.stat_pull_down_fresh && t.onPullDownRefresh && function() {
                var a = t.onPullDownRefresh;
                t.onPullDownRefresh = function() {
                    u.Event.stat(s.prefix + "pulldownfresh", {
                        url: t.__route__
                    }), a.apply(this, arguments);
                };
            }(), s.stat_reach_bottom && t.onReachBottom && function() {
                var a = t.onReachBottom;
                t.onReachBottom = function() {
                    u.Event.stat(s.prefix + "reachbottom", {
                        url: t.__route__
                    }), a.apply(this, arguments);
                };
            }(), s.stat_share_app && t.onShareAppMessage && function() {
                var a = t.onShareAppMessage;
                t.onShareAppMessage = function() {
                    return u.Event.stat(s.prefix + "shareapp", {
                        url: t.__route__
                    }), a.apply(this, arguments);
                };
            }();
        },
        multiStat: function(t, a) {
            if (1 == a) u.Page.stat(t); else {
                var e = getCurrentPages()[getCurrentPages().length - 1];
                e.onShow && function() {
                    var a = e.onShow;
                    e.onShow = function() {
                        u.Page.stat(t), a.call(this, arguments);
                    };
                }();
            }
        },
        stat: function(t) {
            if ("" != s.app_id) {
                var a = [], e = o();
                if (t && (e.r2 = t), t = [ n(), e, r() ], u.Data.lanchInfo) {
                    t.push({
                        ht: u.Data.lanchInfo.scene
                    }), u.Data.pageQuery && u.Data.pageQuery._mta_ref_id && t.push({
                        rarg: u.Data.pageQuery._mta_ref_id
                    });
                    try {
                        1 == u.Data.lanchInfo.landing && (e.ext += ";lp=1", u.Data.lanchInfo.landing = 0);
                    } catch (t) {}
                }
                t.push({
                    rdm: "/",
                    rurl: 0 >= u.Data.lastPageUrl.length ? u.Data.pageUrl + i(u.Data.lastPageQuery) : encodeURIComponent(u.Data.lastPageUrl + i(u.Data.lastPageQuery))
                }), t.push({
                    rand: +new Date()
                }), e = 0;
                for (var p = t.length; e < p; e++) for (var l in t[e]) t[e].hasOwnProperty(l) && a.push(l + "=" + (void 0 === t[e][l] ? "" : t[e][l]));
            }
        }
    },
    Event: {
        stat: function(t, a) {
            if ("" != s.event_id) {
                var e = [], i = n(), u = o();
                i.dm = "wxapps.click", i.url = t, u.r2 = s.event_id;
                var p, l = void 0 === a ? {} : a, c = [];
                for (p in l) l.hasOwnProperty(p) && c.push(encodeURIComponent(p) + "=" + encodeURIComponent(l[p]));
                for (l = c.join(";"), u.r5 = l, l = 0, u = (i = [ i, u, r(), {
                    rand: +new Date()
                } ]).length; l < u; l++) for (var h in i[l]) i[l].hasOwnProperty(h) && e.push(h + "=" + (void 0 === i[l][h] ? "" : i[l][h]));
            }
        }
    },
    Data: {
        userInfo: null,
        lanchInfo: null,
        pageQuery: null,
        lastPageQuery: null,
        pageUrl: "",
        lastPageUrl: "",
        show: !1
    }
};

module.exports = u;